package com.etoak.commons.config;

import com.etoak.commons.PicProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Autowired
    PicProperties picProperties;
    /**
     * 在mvc文件中配置的可以实现这个方法
     * <mvc:resource lovcation="" mapping=""></mvc:resource>
     * 访问图片
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //如果上传路径是以/结尾  直接使用，，如不是加上/
        String location = picProperties.getLocation().endsWith("/")?
                picProperties.getLocation() :
                picProperties.getLocation()+"/";
        //如果访问路径是以/结尾  那就追加上**  如果不是就追加/**
        String mapping = picProperties.getMapping().endsWith("/")?
                picProperties.getMapping() +"**":
                picProperties.getMapping()+"/**";

        registry.addResourceHandler(mapping) //mapping
        .addResourceLocations("file:"+location);//location


    }
}
